package cn.tsvtc.health.service.impl;


import cn.tsvtc.health.common.entity.PageResult;
import cn.tsvtc.health.common.entity.QueryPageBean;
import cn.tsvtc.health.mapper.CheckItemMapper;
import cn.tsvtc.health.pojo.CheckItem;
import cn.tsvtc.health.service.CheckItemService;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author: Athena
 * @date: 2020/11/16 16:29
 * @description:
 */
@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    CheckItemMapper checkItemMapper;

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        if(currentPage == null || currentPage <= 0){
            //非法页码
            currentPage = 1;
        }

        Integer pageSize = queryPageBean.getPageSize();
        if(pageSize == null || pageSize <= 0){
            //非法页大小
            pageSize = 10;
        }

        String queryString = queryPageBean.getQueryString();

        if(queryString == null) {
            queryString = "";
        } else if(!StringUtils.isBlank(queryString)){
            //不是null也不是""，有可能是 "   "
            queryString = queryString.trim();
        }

        queryString = "%" + queryString + "%";

        //queryString 一定有值   "%%"
        PageHelper.startPage(currentPage, pageSize);
        List<CheckItem> list = checkItemMapper.findByCondition(queryString);
        //分页对象
        PageInfo<CheckItem> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 获取所有的信息
     *
     * @return
     */
    @Override
    public List<CheckItem> getAll() {
        List<CheckItem> list = checkItemMapper.selectAll();
        return list;
    }

    @Override
    public int delete(Integer id) {
        int result = checkItemMapper.deleteByPrimaryKey(id);
        return result;
    }

    @Override
    public int update(CheckItem checkItem) {
        //如果用户没有修改项目名称[修改其他资料]，让他改
        //1.根据项目名称去查询得到检查项的id跟项目名称【DB】
        Integer id = checkItemMapper.findByName(checkItem.getName());
        //select id,name from t_checkItem where name = "身高"
        //2.判断【查询不到，checkItem的id跟DB中的id一致？】
        if(id == null || id.intValue() == checkItem.getId().intValue()) {
            //让他改
            return checkItemMapper.updateByPrimaryKeySelective(checkItem);
        }
        throw new RuntimeException("项目名称已经存在，请更换.....");

    }

    @Override
    public int insert(CheckItem checkItem) {

        //1.判断编码是否已经存在?
        //SELECT COUNT(*) FROM `t_checkitem` WHERE `code` = '1111'
        int count = checkItemMapper.findByCodeCount(checkItem.getCode());
        if(count > 0) {
            throw new RuntimeException("检查项编码已经存在，请更换编码....");
        }

        //自己完成
        //2.判断名称是否存在?
        //SELECT COUNT(*) FROM `t_checkitem` WHERE `name` = '身高'
        count = checkItemMapper.findByNameCount(checkItem.getName());
        if(count > 0) {
            throw new RuntimeException("检查项名称已经存在，请更换名称....");
        }

        return checkItemMapper.insertSelective(checkItem);

    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id){
        CheckItem checkItem = checkItemMapper.selectByPrimaryKey(id);
        return checkItem;
    }
}
